package com.salesmanager.web.utils.SMS;

import com.salesmanager.core.constants.CacheConstants;
import com.salesmanager.web.constants.Constants;
import com.salesmanager.web.entity.SMSMessage;
import com.salesmanager.web.utils.GenerationIdUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 123 on 2015/9/24.
 */
public class ValidateCode {
    //redis 实现验证码
    public static  Map validate(SMSMessage originMessage,SMSMessage newMessage) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if(originMessage==null){
            map.put("valid","false");
            map.put("msg","验证码错误");
            return  map;
        }
        else {
            if(!ValidateCode.diff2(originMessage)){
                map.put("valid","false");
                map.put("msg","输入超时，请重新发送");
                return  map;
            }
            if(newMessage.getCode().equals(originMessage.getCode())
                    &&newMessage.getType().equals(originMessage.getType())
                    &&newMessage.getPhone().equals(originMessage.getPhone())){
                map.put("valid","true");
            }
            else{
                map.put("valid","false");
                map.put("msg","验证码错误");
            }
        }
        return map;
    }
    public static  Map validate(HttpServletRequest request,String code, String type,String telphone) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        SMSMessage smsMessage=(SMSMessage)request.getSession().getAttribute(Constants.SESSION_SMS);
        if(smsMessage==null){
            map.put("valid","false");
            map.put("msg","验证码错误");
            return  map;
        }
        else {
                if(!ValidateCode.diff2(smsMessage)){
                map.put("valid","false");
                map.put("msg","输入超时，请重新发送");
                return  map;
            }
            if(code.equals(smsMessage.getCode())&&type.equals(smsMessage.getType())
                    &&telphone.equals(smsMessage.getPhone())){
                map.put("valid","true");

            }
            else{
                map.put("valid","false");
                map.put("msg","验证码错误");
            }
        }
        return map;
    }
    public static void delete(HttpServletRequest request) {
        //使验证码失效
        SMSMessage smsMessage=(SMSMessage)request.getSession().getAttribute(Constants.SESSION_SMS);
        smsMessage.setCode(GenerationIdUtil.generateShortUuid());
        request.getSession().setAttribute(Constants.SESSION_SMS,smsMessage);
    }
    public static  boolean diff(SMSMessage smsMessage){
        Date one=smsMessage.getCreateAt();
        Date two=new Date();

        long time1 = one.getTime();
        long time2 = two.getTime();
        long sec;
        long diff=time2-time1;
        sec = (diff/1000);

        if(sec<smsMessage.getTimeOutSeconds()){
            return  false;
        }
        return  true;
    }
    private static  boolean diff2(SMSMessage smsMessage){
        Date one=smsMessage.getCreateAt();
        Date two=new Date();

        long time1 = one.getTime();
        long time2 = two.getTime();
        long sec ;
        long diff=time2-time1;
        sec = (diff/1000);
        if(sec<smsMessage.getValidateTimeoutSeconds()){
            return  true;
        }
        return  false;
    }

}
